﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PessMvc.Helpers;
using PessModel;
using System.Web.Security;

namespace PessMvc.Controllers
{
    public class GradeRuleVerificationController : Controller
    {
        private PESSEntities db;
        public GradeRuleVerificationController()
        {
            if(db==null)
            {
                db = new PESSEntities();
            }
        }
        // GET: DefaultGradRuleVerification
        [Authorize(Roles = "Admin")]
        public ActionResult Index(int activityID)
        {
            var vlist = from v in db.GradeRuleVerifications
                        where v.ActivityID == activityID //&& v.IsVerified==true
                        select v;
            return View(vlist.ToList());
        }
        [Authorize(Roles = "ExpertJudge")]
        public ActionResult VerifyGradeRules(int activityID)
        {
            var gradeHistory = Utility.GetGradeHistory(activityID);
            ViewBag.ActivityID = activityID;
            return View(gradeHistory);
        }
        [Authorize(Roles = "ExpertJudge")]
        [HttpPost, ActionName("VerifyGradeRules")]
        public ActionResult VerifyGradeRulesConfirmed(int activityID)
        {
            bool isPassed = false;
            string errorMsg = "";
            int violationsCount;
            var gradeHistory = Utility.GetGradeHistory(activityID);
            //Unless the expert has graded at least one archive, otherwise there is nothing to confirmed.
            string expertSN = Membership.GetUser().UserName.ToLower().Trim();
            //The number of grades this expert has assigned, in this activity.
            var gradedArchiveCount = (from g in db.Grades                                      
                                     where g.GradeRule.Subject.ActivityID==activityID && g.ExpertSN.ToLower().Trim() == expertSN
                                     select g).Count();
            if (gradedArchiveCount>0)
            {
                violationsCount = (from record in gradeHistory
                                       where record.GradeRecords.Where(
                                           gr =>gr.IsValidationRequired==true 
                                           &&  gr.Count > Decimal.ToInt32(gr.AllowedMaximum) 
                                           ).Count() > 0
                                       select record
                                   ).Count();
                if(violationsCount==0)
                {
                    isPassed = true;
                }    
                else
                {
                    errorMsg = string.Format("有{0}条等第规则超过规定值！请返回修改。",violationsCount);
                }
            }
            else
            {
                errorMsg = "在当前活动中，您还没有提交任何等第记录！";
            }
            ViewBag.ActivityID = activityID;
            if (isPassed)
            {
                var gradeRuleVerifi=db.GradeRuleVerifications.Where(v => v.ActivityID == activityID && v.ExpertSN.ToLower() == expertSN).FirstOrDefault();
                if(gradeRuleVerifi == null)
                {
                    db.GradeRuleVerifications.Add(new GradeRuleVerification() { ActivityID = activityID, ExpertSN = expertSN, IsVerified = true });
                }
                else
                {
                    gradeRuleVerifi.IsVerified = true;
                    db.Entry(gradeRuleVerifi).State = System.Data.Entity.EntityState.Modified;
                }
                db.SaveChanges();
                return RedirectToAction("Index", "Archive", new { activityID = activityID });
            }
            else
            {
                ModelState.AddModelError("", errorMsg);
                return View(gradeHistory);
            }
        }
        [Authorize(Roles = "Admin")]
        public ActionResult Revoke(int activityID,string expertSN)
        {
            GradeRuleVerification verifi = db.GradeRuleVerifications.Find(activityID, expertSN);
            ViewBag.ActivityID = activityID;
            return View(verifi);
        }
        [Authorize(Roles = "Admin")]
        [HttpPost, ActionName("Revoke")]
        public ActionResult RevokeConfirmed(int activityID, string expertSN)
        {
            GradeRuleVerification verifi = db.GradeRuleVerifications.Find(activityID, expertSN);
            if(verifi!=null)
            {
                verifi.IsVerified = false;
                db.Entry(verifi).State = System.Data.Entity.EntityState.Modified;                
                db.SaveChanges();                
            }
            return RedirectToAction("Index", "GradeRuleVerification", new { activityID = activityID });
        }
    }
}